import { Node, Widget } from "cc";
import EventManager from "../../Common/EventManager";
import { EventType } from "../../GameSpecial/GameEventType";
import { NATIVE_AD_STYLE, NATIVE_AD_TYPE } from "./HeyGameSDK/GlobalData";
import HeyGamePlatform from "./HeyGameSDK/HeyGamePlatform";
import SDK from "./SDK";

/**白鹰SDK-OPPO平台 */
export default class HeyGameOPPOSDK extends SDK {
    protected apiName: string = 'qg';
    public sceneid = 0;
    protected channel_id;

    public init(data?) {
        this.api = window[this.apiName];

        // this.systemInfo = this.api.getSystemInfoSync();

        // console.log("系统信息：");
        // console.log(JSON.stringify(this.systemInfo));

        // // this.api.onShareAppMessage(() => ({}));
        // this.api.showShareMenu({ withShareTicket: false });

        HeyGamePlatform.instance.init();

        // if (!!data) {
        //     HeyGameConfig.HeyGameSDKConfig.instance["_config"]["appId"] = data.appId;
        //     HeyGameConfig.HeyGameSDKConfig.instance["_config"]["versionName"] = data.heyVersionName;
        //     HeyGameConfig.HeyGameSDKConfig.instance["_config"]["bannerAdUnitId"] = data.BannerAdUnitId;
        //     HeyGameConfig.HeyGameSDKConfig.instance["_config"]["interstitialAdUnitId"] = data.InterstitialAdUnitId.length > 0 ? data.InterstitialAdUnitId[0] : "";
        //     HeyGameConfig.HeyGameSDKConfig.instance["_config"]["videoAdUnitId"] = data.videoAdUnitId.length > 0 ? data.videoAdUnitId[0] : "";
        //     HeyGameConfig.HeyGameSDKConfig.instance["_config"]["nativeAdUnitId"] = data.heyNativeAdUnitId;
        //     HeyGameConfig.HeyGameSDKConfig.instance["_config"]["drawerGameAdUnitId"] = data.heyDrawerGameAdUnitId;
        //     HeyGameConfig.HeyGameSDKConfig.instance["_config"]["bannerGameAdUnitId"] = data.heyBannerGameAdUnitId;
        // }

        EventManager.emit(EventType.Common.SDKEvent.inited);
    }

    public onEvents() {
        // EventManager.on(EventType.Common.SDKEvent.showMsg, this.showMessage, this);

        //banner广告
        // EventManager.on(EventType.Common.SDKEvent.showBanner, this.showBanner, this);
        // EventManager.on(EventType.Common.SDKEvent.hideBanner, this.removeBanner, this);
        // EventManager.on(EventType.Common.SDKEvent.checkVisibleBanner, this.checkVisibleBanner, this);

        EventManager.on(EventType.Common.SDKEvent.showVideo, this.showVideo, this);

        //录屏功能
        // EventManager.on(EventType.Common.SDKEvent.startRecord, this.startRecord, this);
        // EventManager.on(EventType.Common.SDKEvent.pauseRecord, this.pauseRecord, this);
        // EventManager.on(EventType.Common.SDKEvent.resumeRecord, this.resumeRecord, this);
        // EventManager.on(EventType.Common.SDKEvent.stopRecord, this.stopRecord, this);
        // EventManager.on(EventType.Common.SDKEvent.shareRecord, this.shareRecord, this);

        //插屏
        EventManager.on(EventType.Common.SDKEvent.showInsertAd, this.showInterstitialAd, this);
        // EventManager.on(EventType.Common.SDKEvent.showInsertByPauseLevel, this.showInsertAdByPauseLevel, this);

        //跳转
        // EventManager.on(EventType.Common.SDKEvent.navigateToMiniProgram, this.navigateToMiniProgram, this);

        //震动
        EventManager.on(EventType.Common.SDKEvent.vibrateLong, this.onVibrateLong, this);
        EventManager.on(EventType.Common.SDKEvent.vibrateShort, this.onVibrateShort, this);

        //释放垃圾内存
        EventManager.on(EventType.Common.SDKEvent.triggerGC, this.triggerGC, this);

        //原生广告
        EventManager.on(EventType.Common.SDKEvent.showNativeAd, this.showNativeAd, this);
        // EventManager.on(EventType.Common.SDKEvent.hideNativeAd, this.hideNativeAd, this);
        EventManager.on(EventType.Common.SDKEvent.hideAllNativeAd, this.hideAllNativeAd, this);

        //分享
        EventManager.on(EventType.Common.SDKEvent.shareToFriends, this.shareToAnyOne, this);

        //OPPO平台添加桌面图标：
        EventManager.on(EventType.Common.SDKEvent.getTableIconState, this.getTableIconState, this);
        EventManager.on(EventType.Common.SDKEvent.addTableIcon, this.addTableIcon, this);

        //白鹰SDK-原生广告
        EventManager.on(EventType.Common.HeyGameSDK.initAdNode, this.initAdSDK, this);
        EventManager.on(EventType.Common.HeyGameSDK.showFinishGameBannerAd, this.showFinishGameBanner, this);
        EventManager.on(EventType.Common.HeyGameSDK.hideFinishGameBannerAd, this.hideFinishGameBanner, this);
        EventManager.on(EventType.Common.HeyGameSDK.clickNativeAd, this.onClickNativeAd, this);
        //白鹰SDK-数据统计
        EventManager.on(EventType.Common.ALDEvent.levelStart, this.onLevelStart, this);
        EventManager.on(EventType.Common.ALDEvent.levelWin, this.onLevelWin, this);
        EventManager.on(EventType.Common.ALDEvent.levelLose, this.onLevelLose, this);
        EventManager.on(EventType.Common.ALDEvent.levelExit, this.onLevelExit, this);
        //白鹰SDK-互推广告
        EventManager.on(EventType.Common.HeyGameSDK.showMoreGame, this.showMoreGame, this);
    }

    /**初始化显示原生广告的节点（需在游戏中一直存在） */
    public initAdSDK(adNode: Node) {
        HeyGamePlatform.instance.initAdSDK(adNode, NATIVE_AD_STYLE.BIG_AD);
    }

    /***********************************************************************激励视频***********************************************************************/
    /**播放视频 */
    public showVideoAd(videoName?: any) {
        console.log("播放视频,301");
        HeyGamePlatform.instance.showVideoAd(301, (status: any) => {
            //播放成功
            if (status) {
                this.onVideoSuccess();
            } else {
                this.onVideoQuit();
                this.onVideoHide();
            }
        });
    }

    /***********************************************************************banner广告***********************************************************************/
    public showBanner(data: { id: string, bannerID: string, style: any }) {

    }

    /***********************************************************************插屏广告***********************************************************************/
    /**插屏 */
    public showInterstitialAd(location) {
        console.log("显示插屏,201");
        HeyGamePlatform.instance.showInsertAd(201);
    }

    /***********************************************************************原生广告***********************************************************************/
    /**当前显示的原生广告的类型，NATIVE_AD_TYPE.GAME/FINISH */
    protected curNativeAdType = null;
    public showNativeAd(data) {
        if (!data.visible) {
            this.hideAllNativeAd();
            return;
        }
        switch (data.style) {
            case NATIVE_AD_STYLE.BIG_AD:
            case "BIG_AD": {
                data.style = NATIVE_AD_STYLE.BIG_AD;
                break;
            }
            case NATIVE_AD_STYLE.BANNER_AD:
            case "BANNER_AD": {
                data.style = NATIVE_AD_STYLE.BANNER_AD;
                break;
            }
            default: {
                console.warn("未定义的原生广告样式:", data.style);
                return;
            }
        }
        switch (data.type) {
            case "GAME": {
                this.hideFinishNativeAd();
                this.showGameNativeAd(data);
                break;
            }
            case "FINISH": {
                this.hideGameNativeAd();
                this.showFinishNativeAd(data);
                break;
            }
            default: {
                console.warn("未定义的原生广告类型:", data.type);
                return;
            }
        }
    }
    /**
     * 显示首页/游戏中的原生广告 OPPO,VIVO
     * @param data.style    原生广告类型，原生大图和原生banner
     * @param data.widget   广告适配参数
     */
    public showGameNativeAd(data: { style: any, widget?: any }) {
        console.log("显示游戏中原生广告", data);
        HeyGamePlatform.instance.showGameNativeAd(data.style, null, null, data.widget);
        this.curNativeAdType = NATIVE_AD_TYPE.GAME;
    }
    /**隐藏原生广告 */
    public hideGameNativeAd() {
        console.log("隐藏游戏中原生广告");
        HeyGamePlatform.instance.hideGameNativeAd();
        this.curNativeAdType = null;
    }

    /**
     * 显示结算页的原生广告
     * @param data.style    原生广告类型，原生大图和原生banner
     * @param data.widget   广告适配参数
     */
    public showFinishNativeAd(data: { style: any, widget?: any }) {
        console.log("显示结算页原生广告");
        HeyGamePlatform.instance.showFinishNativeAd(data.style, null, null, data.widget);
        this.curNativeAdType = NATIVE_AD_TYPE.FINISH;
    }
    public hideFinishNativeAd() {
        console.log("隐藏结算页原生广告");
        HeyGamePlatform.instance.hideFinishNativeAd();
        this.curNativeAdType = null;
    }

    public showFinishGameBanner() {
        console.log("显示结算页banner广告");
        HeyGamePlatform.instance.showFinishGameBannerAd();
    }
    public hideFinishGameBanner() {
        console.log("隐藏结算页banner广告");
        HeyGamePlatform.instance.hideFinishGameBannerAd();
    }

    public hideAllNativeAd() {
        this.hideFinishNativeAd();
        this.hideGameNativeAd();
    }

    /**点击查看按钮，查看原生广告 */
    public onClickNativeAd() {
        console.log("点击按钮查看原生广告");
        if (null === this.curNativeAdType) {
            return;
        }
        switch (this.curNativeAdType) {
            case NATIVE_AD_TYPE.GAME: {
                if (HeyGamePlatform.instance["gameNativeAd"]) {
                    HeyGamePlatform.instance["gameNativeAd"].onNativeAdClick();
                }
                break;
            }
            case NATIVE_AD_TYPE.FINISH: {
                if (HeyGamePlatform.instance["finishNativeAd"]) {
                    HeyGamePlatform.instance["finishNativeAd"].onNativeAdClick();
                }
                break;
            }
        }
    }

    /***********************************************************************硬件功能***********************************************************************/
    /**长震动 */
    public onVibrateLong() {
        HeyGameAdSDK.GameAdSDK.SDK.vibrateLong();
    }
    /**短震动 */
    public onVibrateShort() {
        HeyGameAdSDK.GameAdSDK.SDK.vibrateShort();
    }

    /***********************************************************************桌面图标***********************************************************************/
    /**检测是否已添加桌面图标 */
    public getTableIconState(cb: (haveIcon) => void) {
        HeyGameAdSDK.GameAdSDK.SDK.hasShortcutInstalled((res) => {
            cb(res);
        });
    }
    /**添加桌面图标 */
    public addTableIcon(success: Function, fail?: Function) {
        HeyGamePlatform.instance.addIconToDesktop((res: any) => {
            if (res) {
                success();
            } else if (!!fail) {
                fail();
            }
        });
    }

    /***********************************************************************互推广告***********************************************************************/
    /**展示九宫格广告 */
    public showMoreGame() {
        console.log("显示九宫格");
        HeyGamePlatform.instance.showMoreGame();
    }

    /***********************************************************************游戏事件***********************************************************************/
    /**
     * 关卡开始
     * @param data 
     * @param data.lv   进入的关卡序号
     */
    public onLevelStart(data: { lv: number } | number) {
        console.log("HEY_SDK收到关卡【开始】事件");
        if (typeof data === "number") {
            HeyGamePlatform.instance.onLevelBegin(data);
        } else {
            HeyGamePlatform.instance.onLevelBegin(data.lv);
        }
        HeyGamePlatform.instance.startGameEvent();
    }

    /**
     * 关卡胜利
     * @param data 
     * @param data.lv   进入的关卡序号
     */
    public onLevelWin(data: { lv: number }) {
        console.log("HEY_SDK收到关卡【胜利】事件");
        if (typeof data === "number") {
            HeyGamePlatform.instance.onLevelCompleted(data);
        } else {
            HeyGamePlatform.instance.onLevelCompleted(data.lv);
        }
    }

    /**
     * 关卡失败
     * @param data 
     * @param data.lv   关卡序号
     */
    public onLevelLose(data: { lv: number }) {
        console.log("HEY_SDK收到关卡【失败】事件");
        if (typeof data === "number") {
            HeyGamePlatform.instance.onLevelFailed(data, false);
        } else {
            HeyGamePlatform.instance.onLevelFailed(data.lv, false);
        }
    }
    /**
     * 关卡中途退出
     * @param data 
     * @param data.lv   关卡序号
     */
    public onLevelExit(data: { lv: number }) {
        console.log("HEY_SDK收到关卡【退出】事件");
        if (typeof data === "number") {
            HeyGamePlatform.instance.onLevelFailed(data, true);
        } else {
            HeyGamePlatform.instance.onLevelFailed(data.lv, true);
        }
    }


}
